package farmers.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReadUtil {


    public static void main(String[] args) throws Exception {

        // 文件夹路径
        readFile_2();
//        System.out.println(result);
//        writeFile(path, result.toString());
    }

    public static void writeFile(String fileName, String fileContent) {
        try {
            File f = new File(fileName);
            if (!f.exists()) {
                f.createNewFile();
            }
            OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f), "gbk");
            BufferedWriter writer = new BufferedWriter(write);
            writer.write(fileContent);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *
     */
    public static StringBuffer readFile_1() throws IOException {
        String path = "D:\\root\\IdeaProjects_self\\farmers\\zUtil\\src\\main\\resources\\1.txt";

        // 使用一个字符串集合来存储文本中的路径 ，也可用String []数组
        StringBuffer result = new StringBuffer();
//        File file = new File(path);
//        System.out.println(file.getAbsolutePath());
        FileInputStream fis = new FileInputStream(path);
        // 防止路径乱码 如果utf-8 乱码 改GBK eclipse里创建的txt 用UTF-8，在电脑上自己创建的txt 用GBK
        InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
        BufferedReader br = new BufferedReader(isr);
        String line = "";
        while ((line = br.readLine()) != null) {
//            System.out.println(line);

            String[] split = line.split(" ");
            if(split != null && split.length > 0){
                String param_0 = split[0];
                String param_1 = split[1];
                String param_2 = split[2];

                System.out.println("TYPE_" + param_0 + "(\"" + param_0 + "\", \"" + param_1 + "\", \"" + param_2 + "\"),");
            }


        }
        br.close();
        isr.close();
        fis.close();
        return result;
    }

    /**
     * 从excel提交历史里 分析
     */
    public static Object readFile_2() throws IOException {
        String path = "D:\\root\\IdeaProjects_self\\farmers\\zUtil\\src\\main\\resources\\2.txt";

        // 使用一个字符串集合来存储文本中的路径 ，也可用String []数组
        StringBuffer result = new StringBuffer();

        Map<String, Map<String, String>> map = new HashMap<>();


        FileInputStream fis = new FileInputStream(path);
        // 防止路径乱码 如果utf-8 乱码 改GBK eclipse里创建的txt 用UTF-8，在电脑上自己创建的txt 用GBK
        InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
        BufferedReader br = new BufferedReader(isr);
        String line = "";
        while ((line = br.readLine()) != null) {
//            System.out.println(line);

            String[] split = line.split(" ");
            if(split != null && split.length > 0){
                String param_0 = split[0];
                String param_1 = split[1];
                String param_2 = split[2];

                System.out.println("TYPE_" + param_0 + "(\"" + param_0 + "\", \"" + param_1 + "\"" + "),");
            }
        }
        br.close();
        isr.close();
        fis.close();

        map.forEach((x,y)->{
            System.out.println("=================" + x);
            y.forEach((a, b) -> {
                System.out.println(b);
            });
        });


        return result;
    }


    public static List<String> readFile(String path) {
        // 使用一个字符串集合来存储文本中的路径 ，也可用String []数组
        List<String> result = new ArrayList();
        Map<String, Map<String, String>> map = new HashMap<>();

        try {
            FileInputStream fis = new FileInputStream(path);
            // 防止路径乱码 如果utf-8 乱码 改GBK eclipse里创建的txt 用UTF-8，在电脑上自己创建的txt 用GBK
            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String line = "";
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                result.add(line);

//            if("".equals(line)){
//                continue;
//            }
            }
            br.close();
            isr.close();
            fis.close();
        }catch (Exception e){
            e.printStackTrace();
        }

        return result;
    }
}
